﻿// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;


namespace eProject.Web.UI
{

    public partial class EditUserPage : System.Web.UI.Page
    {
        private Int64 userId = -1;
        private Users user;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadParams();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (IsPostBack) return;
            BindUsers();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.userId > 0)
                    user = new Users(this.userId);
                else
                    Response.Redirect("UserManagement.aspx");
                user.UserName = txtUserName.Text.Trim().ToString();
                user.FirstName = txtFirstName.Text.Trim().ToString();
                user.LastName = txtLastName.Text.Trim().ToString();
                user.DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text.Trim().ToString());
                user.Email = txtEmail.Text.Trim().ToString();
                user.Address = txtAddress.Text.Trim().ToString();
                user.Gender = (Convert.ToInt32(rdGender.SelectedValue) > 0);
                user.PhoneNumber = txtPhoneNumber.Text.Trim().ToString();
                user.Roles = 1;
                user.Save();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                user = null;
            }
        }

        private void BindUsers()
        {
            if (this.userId > 0)
                user = new Users(this.userId);
            else
                Response.Redirect("UserManagement.aspx");

            txtUserName.Text = user.UserName;
            txtFirstName.Text = user.FirstName;
            txtLastName.Text = user.LastName;
            txtDateOfBirth.Text = user.DateOfBirth.ToString("dd/MM/yyyy");
            txtEmail.Text = user.Email;
            txtAddress.Text = user.Address;
            rdGender.ClearSelection();
            ListItem listItem = rdGender.Items.FindByValue(Convert.ToInt32(user.Gender).ToString());
            listItem.Selected = true;
            txtPhoneNumber.Text = user.PhoneNumber;
            txtDistance.Text = user.Distance.ToString();
            ltCreatedDate.Text = user.CreatedDate.ToString("dd/MM/yyyy");
            ltLastUpdate.Text = user.LastUpdate.ToString("dd/MM/yyyy");
        }

        private void PopulateLabels()
        {
            if (IsPostBack) return;
            Title = "Edit User Page - eProject";
            ltHeader.Text = "Edit User";
            lblUser.Text = "User Name";
            lblEmail.Text = "Email";
            lblFirstName.Text = "First Name";
            lblLastName.Text = "Last Name";
            lblDateOfBirth.Text = "Date Of Birth";
            lblGender.Text = "Gender";
            lblAddress.Text = "Address";
            lblPhoneNumber.Text = "Phone Number";
            lblDistance.Text = "Distance";
            lblCreatedDate.Text = "Created Date";
            lblLastUpdate.Text = "Last Update";
        }

        private void LoadSettings()
        {
            if (IsPostBack) return;
            if (this.userId > 0)
            {
                user = new Users(this.userId);
                if (user.Id != this.userId) return;
                this.userId = user.Id;
            }
            else return;
        }

        private void LoadParams()
        {
            if (Page.Request.QueryString.Count > 0)
            {
                string Id = Page.Request.QueryString["Id"].ToString();
                if (!string.IsNullOrEmpty(Id))
                    this.userId = Convert.ToInt64(Id);
            }
        }

        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnSave.Click += new EventHandler(btnSave_Click);
        }

        #endregion
    }
}